﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Models.Sellers;
using S4Store.Service;
using S4Store.Domain;

namespace S4Store.Controllers
{
    public class SellerController : BaseController
    {

        public EmployeeService SellerService { get; set; }
        public StoreService StoreService { get; set; }

        public ActionResult Index()
        {
            ListPage model = new ListPage();
            using (var t = new Transtion())
            {
                t.Inject(StoreService);
                var stores = StoreService.GetQuery().ToList();
                model.Stores = stores;
            }
            return View(model);
        }

        public ActionResult Query()
        {
            QueryModel query = new QueryModel
                                   {
                                       Page = 1,
                                       PageSize = 20
                                   };
            return Query(query);
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerService);

                var querysource = SellerService.GetQuery();
                if(!Helper.StringIsEmpty(query.Name))
                {
                    querysource = querysource.Where(x => x.Name.Contains(query.Name));
                }
                if (!Helper.StringIsEmpty(query.StoreCode))
                {
                    querysource = querysource.Where(x => x.Store.Code.Contains(query.StoreCode));
                }
                if ((

                           CurrentUser.Position == S4Store.Domain.Position.GeneralManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager) 
                           )
                {
                    if(query.Store != null){
                        querysource = querysource.Where(x => x.StoreId == query.Store);
                    }
                    if (query.Pinpai != null)
                    {
                        querysource = querysource.Where(x =>x.Store!=null&& x.Store.PinPaiId == query.Pinpai.Value);
                    }
                }
                else
                {
                    int storeid = CurrentUser.StoreId.Value;
                    querysource = querysource.Where(x => x.StoreId == storeid);
                }
                if (query.EmployeeType != null)
                {
                    querysource = querysource.Where(x => x.TypeValue == query.EmployeeType.Value);
                }
                if (query.Status != null)
                {
                    querysource = querysource.Where(x => x.StatusValue == query.Status.Value);
                }
                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => x.Id);
                if(query.Page>1&&query.PageSize>0)
                {
                    querysource = querysource.Skip((query.Page - 1)*query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
               LX.Core.Query.PagedList<Employee> data = new LX.Core.Query.PagedList<Employee>
                                 {
                                     Data = querysource.ToList(),
                                     Page = query.Page,
                                     PageSize = query.PageSize,
                                     Total = total

                                 };
               return View(data);
            } 
        }

        public ActionResult Add()
        {
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                var stores = StoreService.GetQuery().ToList();
                if (CurrentUser.Position != S4Store.Domain.Position.SystemManager)
                {
                    stores = stores.Where(x => x.Id == CurrentUser.StoreId).ToList();
                }
                t.Commit();
                return View(stores);
            } 
        }

        [HttpPost]
        public JsonResult Add(Employee model)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerService);
                SellerService.AddEmployee(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success=true
                    }
                };
            } 
        }

        [HttpPost]
        public ActionResult Modify(int id)
        {
            using (var t = new Transtion())
            {
                t.Inject<EmployeeService>(SellerService);
                t.Inject<StoreService>(StoreService);
                var user = SellerService.GetQuery().FirstOrDefault(x => x.Id == id);
                var stores = StoreService.GetQuery().ToList();
                if (CurrentUser.Position != S4Store.Domain.Position.SystemManager)
                {
                    stores = stores.Where(x => x.Id == CurrentUser.StoreId).ToList();
                }
                t.Commit();
                if (user != null)
                {
                    Combin<List<Store>, Employee> model = new Combin<List<Store>, Employee>
                    {
                        Data1 = stores,
                        Data2 = user
                    };
                    return View(model);
                }
                return this.Content("销售顾问不存在");
            }   
            
        }


        [HttpPost]
        public JsonResult ModifyBack(Employee model)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerService);
                SellerService.ModifyEmployee(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }

        }

        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            if(Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject(SellerService);
                foreach (var id in ids)
                {
                    SellerService.DeleteEmployee(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    
    }
}
